<?php


namespace App\Service\Store;


use App\Dao\UserBankDao;
use App\Dao\UserCashDao;
use App\Dao\UserDao;
use Hyperf\Di\Annotation\Inject;

/**
 * 提现服务
 * @author 陈奕文
 * @package App\Service\Store
 */
class WithdrawService
{
    /**
     * 用户
     * @Inject()
     * @var UserDao
     */
    public $user_dao;

    /**
     * 用户提现记录
     * @Inject()
     * @var UserCashDao
     */
    public $dao;

    /**
     * @Inject
     * @var UserBankDao
     */
    private $user_bank_dao;

    public function getUserCashList(array $params)
    {
        $map = [];
        isset($params['start_time']) && !isset($params['end_time'])  && !empty($params['start_time']) && $map['created_at'] = ['>=', strtotime($params['start_time'])]; // 创建开始时间
        isset($params['end_time']) &&  !isset($params['start_time'])  && !empty($params['end_time']) && $map['created_at'] = ['<=', strtotime($params['end_time'])]; // 创建结束时间

        isset($params['end_time']) &&  isset($params['start_time']) &&
        !empty($params['end_time']) && !empty($params['start_time']) &&
        $map['between_field'] = ['created_at' =>[strtotime($params['start_time']), strtotime($params['end_time'])]]; // 有创建开始和结束时间

        isset($params['status']) && $map['status'] = (int)$params['status']; // 提现状态/0待处理/1已处理/2已拒绝

        // 姓名  手机号
        if (isset($params['name']) || isset($params['phone'])) {
            $user_map = [];
            isset($params['name']) && $user_map['name'] = ['like', "%{$params['name']}%"]; // 姓名
            isset($params['phone']) && $user_map['phone'] = ['like', "%{$params['phone']}%"]; // 手机号
            $select = ['id', 'name', 'phone'];
            $users = $this->user_dao->getUsersByMap($user_map, null, $select)->toArray();
            if (!empty($users)) {
                $ids = array_column($users, 'id');
                $map['user_id'] = ['in', $ids]; // 用户ID
            } else { // 未查到用户
                $map['user_id'] = ['in', $users];
                // 分页
                $page_size = null;
                if (isset($params['page'])) {
                    $page_size = 20;
                    isset($params['page_size']) && !empty($page_size) && $page_size = (int)$params['page_size'];
                }
                $sort = 'created_at desc';
                $select = ['id', 'user_id', 'order_no','status', 'created_at', 'amount', 'bank_name', 'bank_account', 'bank_realname', 'bank_idcard', 'bank_phone', 'remark', 'bank_type'];
                $user_cash_list = $this->dao->getUserCashByMap($map, $sort, $select, $page_size)->toArray();
                return $user_cash_list;
            }
        }
        // 搜索 提现银行 银行卡号 提现人姓名 提现人身份证
        isset($params['search']) && !empty($params['search'])
        && $map['or_field'] = [
            'bank_name' => ['like',"%{$params['search']}%"], // 提现银行
            'bank_account' => ['like',"%{$params['search']}%"], // 银行卡号
            'bank_realname' => ['like',"%{$params['search']}%"], // 提现人姓名
            'bank_idcard' => ['like',"%{$params['search']}%"], // 提现人身份证
        ];
        // 分页
        $page_size = null;
        if (isset($params['page'])) {
            $page_size = 20;
            isset($params['page_size']) && !empty($page_size) && $page_size = (int)$params['page_size'];
        }
        $sort = 'created_at desc';
        $select = ['id', 'user_id', 'order_no','status', 'created_at', 'amount', 'bank_name', 'bank_account', 'bank_realname', 'bank_idcard', 'bank_phone', 'remark', 'bank_type'];

        $user_cash_list = $this->dao->getUserCashByMap($map, $sort, $select, $page_size)->toArray();

        if (isset($user_cash_list['data'])) {
            $user_cash_list['data'] = $this->joinData($user_cash_list['data']);
        } else {
            $user_cash_list = $this->joinData($user_cash_list);
        }

        return $user_cash_list;
    }

    /**
     * 拼接数据
     * @param array $user_cash_list
     * @return array
     */
    public function joinData(array $user_cash_list)
    {
        // 获取用户账号
        $user_ids = array_unique(array_column($user_cash_list, 'user_id'));
        $user_select = ['id', 'phone', 'name'];
        $users = $this->user_dao->getUsersByIdsNotGroup($user_ids, $user_select);
        $phones = $users->pluck('phone', 'id');
        $names = $users->pluck('name', 'id');

        foreach ($user_cash_list as &$item) {
            $item['create_time'] = date('Y-m-d H:i:s', (int)$item['created_at']); // 创建时间
            $item['phone'] = $phones[$item['user_id']]; // 手机号/账号
            $item['name'] = $names[$item['user_id']]; // 用户名/昵称

            $bank = $this->user_bank_dao->map([
                'user_id' => $item['user_id'],
                'bank_account' => $item['bank_account'],
            ],false);

            $item['bank_branch'] = $bank['bank_branch'] ?? ''; // 支行
        }

        return $user_cash_list;
    }

}